var app = getApp()
var gb = app.globalData
var Define = require("../../lib/Define.js")

Page({
  data: {
    imgUrls: [
      'http://opfdgzihi.bkt.clouddn.com/IMG_1083.jpg',
      'http://opfdgzihi.bkt.clouddn.com/KW0A2472.jpg',
      'http://opfdgzihi.bkt.clouddn.com/o_1bf9iedl314111jp91nmv10ega9h.jpg'
    ],
    userProductList:[],
    productList:[],
    isLoading:true
  },
  onReady:function(){

    Define.requestBusinessInfo()
    
    this.reqOrderCase()
    this.reqProductList()
  },
  onProCellClick:function(e){
    
  },
  reqOrderCase:function(){
    //获取客片列表
    var self = this

    wx.request({
      url: Define.value.baseUrl+"/app/user_product/list_all?bid="+Define.value.bid+"&limit=4",
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var data = res.data

        var restuls = [];
        for(var i = 0; i < data.data.length; i++){
          var categoryItem = data.data[i]
          for(var j = 0 ; j < categoryItem.beans.length; j++){
            var beanItem = categoryItem.beans[j]
            console.log(beanItem)
            beanItem.mainImage = beanItem.images[0].url
            beanItem.consumer_avatar = beanItem.userAvatar
          }
        }

        if(data.code == 200){
          self.setData({
            userProductList:data.data
          })

          console.log(data.data)
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
    
  },
  reqProductList:function(){
    //获取作品展示列表
    var self = this
    
    wx.request({
      url: Define.value.baseUrl+'/product/list?bid='+Define.value.bid,
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        for(var index = 0 ; index < res.data.data.length ; index++){
          var item = res.data.data[index];
          item.mainImage = item.images[0].url
        }
          self.setData({
            productList:res.data.data,
            isLoading:false
          });
      }
    })
  },
  onShareAppMessage:function(){
    return {
      title: Define.loadBusinessInfo().name,
      path:"/pages/home/page"
    }
  },
  onDIYOfferButtonClick:function(){
    wx.navigateTo({
      url: '../diyOffer/diyOffer',
    })
  }
})